#include "role_fightsquare.h"
#include "Player.h"

#include "../share/string_common.h"

CRoleFightSquare::CRoleFightSquare( )
{
	role_fight_square_.clear();
}

CRoleFightSquare::~CRoleFightSquare( )
{

}

bool CRoleFightSquare::ReadFromJson(const Json::Value& data_json)
{
	for ( int32_t i = 0; i < data_json.size(); ++i )
	{
		Json::Value one_fight = data_json[i];
		int32_t square_type = one_fight["type"].asInt();
		CFightSquare fightsquare;
		fightsquare.ReadFromJson(one_fight["square"]);
		role_fight_square_[square_type] = fightsquare;
	}
	return true;
}

bool CRoleFightSquare::WriteToJson(Json::Value& data_json)
{
	for (auto it = role_fight_square_.begin(); it != role_fight_square_.end(); ++it )
	{
		Json::Value one_square;
		one_square["type"] = (int32_t)it->first;
		Json::Value square;
		it->second.WriteToJson(square);
		one_square["square"] = square;
		
		data_json.append(one_square);
	}
	return true;
}


int32_t CRoleFightSquare::SetSquare(EFightSquareType fight_type, CFightSquare& fightsquare)
{
	role_fight_square_[fight_type] = fightsquare;
	return 0;
}


CFightSquare* CRoleFightSquare::GetSquareByType(EFightSquareType fight_type)
{
	auto it = role_fight_square_.find(fight_type);
	if ( it == role_fight_square_.end() )
	{
		return NULL;
	}
	return &(it->second);
}

CFightSquare::CFightSquare()
{
	memset(fight_square_, 0, sizeof(fight_square_));
}

bool CFightSquare::ReadFromJson(const Json::Value& data_json)
{
	for (int32_t i = 0; i < data_json.size()&& i < MAX_SQUARE_NUM; ++i)
	{
		fight_square_[i] = data_json[i].asInt();
	}
	return true;
}

bool CFightSquare::WriteToJson(Json::Value& data_json)
{
	for (int32_t i = 0; i < MAX_SQUARE_NUM; ++i)
	{
		data_json[i] = fight_square_[i];
	}
	return true;
}


bool CFightSquare::FillToJson(Json::Value& data_json)
{
	for (int32_t i = 0; i < MAX_SQUARE_NUM; ++i )
	{
		std::string index = ::common::ToString(i+1);
		data_json[index] = fight_square_[i];
	}
	return true;
}

bool CFightSquare::makeupFromJson(const Json::Value& data_json)
{
	for ( int32_t i = 0; i < data_json.size()&& i < MAX_SQUARE_NUM; ++i)
	{
		std::string index = ::common::ToString(i+1);
		int32_t heroid = data_json[index].asInt();
		fight_square_[i] = heroid;
	}
	return true;
}

bool CFightSquare::isEmpty()
{
	for (int32_t i = 0; i < MAX_SQUARE_NUM; ++i)
	{
		if (fight_square_[i] != 0)
		{
			return false;
		}
	}
	return true;
}

int32_t CFightSquare::GetSquareHeroId(int32_t index)
{
	if ( index < 0 || index >= MAX_SQUARE_NUM)
	{
		return 0;
	}
	return fight_square_[index];
}
